package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.data.service.user.UserBuyService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseStudyHistory;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/ajax/course")
public class AjaxCourseStudyController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(AjaxCourseStudyController.class);
	@Autowired
	private CourseStudyHistoryService courseStudyHistoryService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private UserBuyService userBuyService;
	@Autowired
	private UserService userService;

	/**
	 * 添加播放次数 ,播放记录
	 */
	@RequestMapping("/study/times.json")
	public AjaxResult addCoursePlayerTimes(HttpServletRequest request) {
		int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
		User user = LoginUser.getUser(request);
		if (user == null) {
			return AjaxResult.auth("用户未登录！");
		}
		// 判断节点不为空
		CourseNode courseNode = courseNodeService.get(nodeId);
		if (ObjectUtils.isNull(courseNode)) {
			return AjaxResult.error("找不到指定课程节点！");
		}
		//未购买不插入记录
		boolean isBuy = userBuyService.checkIsBuy(user.getUserId(), ModuleType.COURSE.ordinal(),courseNode.getCourseId());
		if (!isBuy) {
			return AjaxResult.error("用户未报名该课程！");
		}
		Course course = courseService.get(courseNode.getCourseId());
		// 判断课程不为空
		if (ObjectUtils.isNull(course)) {
			return AjaxResult.error("找不到指定课程！");
		}

		CourseStudyHistory history = new CourseStudyHistory();
		history.setCourseId(courseNode.getCourseId());
		history.setCourseName(course.getCourseName());
		history.setNodeId(nodeId);
		history.setNodeName(courseNode.getName());
		history.setUserId(user.getUserId());
		history.setUserName(user.getDisplayName());
		courseStudyHistoryService.playerTimes(history);
		//修改课程章节的播放次数
		courseNodeService.updatePlayCount(courseNode.getNodeId());
		return AjaxResult.success();
	}

	/**
	 * 学过此课程的用户
	 */
	@RequestMapping("/study/users.json")
	public AjaxResult getCourseLearnedUser(HttpServletRequest request) {
		int courseId = TKConvert.toInteger(request.getParameter("courseId"));
		if (courseId < 1) return AjaxResult.error("未指定课程Id！");
		String KeyPrefix = CacheConstants.COURSE_LEARNED_PREFIX + "count:" + courseId;
		//学习此课程的总人数
		Integer learnedCount = (Integer) CacheUtils.get(KeyPrefix);
		//从缓存中获取
		//学习此课程的人 (最新8个)
		List<User> histories = userService.getStudentList(courseId, new PageRequest(1, 8));
		if (learnedCount == null) {
			learnedCount = userBuyService.countByCourseId(courseId);
			CacheUtils.set(KeyPrefix, learnedCount, 6000);//缓存10分钟
		}
		return AjaxResult.success(histories, learnedCount);
	}
}